/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Environment;
import android.view.KeyEvent;
import android.widget.TableLayout;

/**
 * Debug helper, used to make debugging easier.
 * <p>
 * <b>Thread safety:</b> Is not thread safe, but if it is initialized at factory start up and after that properties will
 * only be read, then it is thread safe.
 *
 * @author 7realm
 * @version 1.0
 */
public final class DebugHelper {
    /**
     * Private constructor for class {@link DebugHelper} to prevent initialization.
     */
    private DebugHelper() {
        // empty
    }

    /** Represents the test keyboard view */
    public static TableLayout keyboardView = null;
    /** Represents whether the performance trace had been enabled */
    public static boolean enableTrace = false;
    /** Represents whether the test keyboard had been enabled */
    public static boolean testKeyboardEnabled = false;
    /** Represents the key code of the test keyboard switch */
    public static final int testKeyboardSwitchCode = KeyEvent.KEYCODE_MENU;
    
    /** Represents file name, were commands will be stored. */
    public static String commandsOutFile;

    /** Represents file name, which contains commands to process. */
    public static String commandsInFile;

    /** Represents test number that will be executed directly. */
    public static int testNumber;

    /** The indicator which tells if XREView bounds will be draw. */
    public static boolean drawViewBounds;

    public static Activity currentActivity = null;
    
    public static int usedImageMemorySize = 0;
    
    public static int imageMemoryPoolSize = 10 * 1024 * 1024;
    
    /**
     * Switch off debugging.
     */
    public static void switchOff() {
        commandsInFile = null;
        commandsOutFile = null;
        testNumber = 0;
        drawViewBounds = false;
    }
    
    private static void writeToFile(String fileName, String content) {
    	File root = Environment.getExternalStorageDirectory();
    	FileWriter writer = null;
    	try {
			writer = new FileWriter(new File(root, fileName), true);
			writer.append(content + "\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				// nothing
			}
		}
    }
    
    public static void recordCommand(AndroidReceiver receiver, String command) {
    	String fileName = receiver.getApplicationData().getSessionGUID() + "_" +
    		receiver.getApplicationConnectData().getApplicationName() + "_command";
		//writeToFile(fileName, command);
    }
    
    public static void recordEvent(AndroidReceiver receiver, String event) {
    	String fileName = receiver.getApplicationData().getSessionGUID() + "_" +
			receiver.getApplicationConnectData().getApplicationName() + "_event";
    	//writeToFile(fileName, event);
    }
    
    public static void recordException(String e) {
    	String fileName = "feynman_exception";
    	//writeToFile(fileName, e);
    }
}
